/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package utfpr.cp.crawler;

import edu.uci.ics.crawler4j.crawler.CrawlController;
import edu.uci.ics.crawler4j.url.URLCanonicalizer;
import edu.uci.ics.crawler4j.url.WebURL;
import java.util.regex.Pattern;
import utfpr.cp.model.URLTarget;

/**
 *
 * @author Adriano_2
 */
public class SingleSiteControl implements ThreadManager {
    CrawlController control;
    int blockedPages;
    final Pattern filter;
    private final CrawlerStats stats;
    final WebURL site;
    private final URLTarget target;
    final boolean hasSubdomain;
    
    public SingleSiteControl(URLTarget target, String ignoredExtension) {
        this.target = target;
        String domain = target.getUrl();
        stats = new CrawlerStats(domain);
        filter = Pattern.compile(".*(\\.(" + ignoredExtension + "))$");
        
        site = new WebURL();
        if (!domain.toLowerCase().startsWith("http")) {
            domain = "http://" + domain;
        }
        site.setURL(URLCanonicalizer.getCanonicalURL(domain));
        hasSubdomain = site.getSubDomain() != null && !site.getSubDomain().toLowerCase().startsWith("www");
        resetBlockedPage();
    }

    @Override
    public boolean shoudVisit(WebURL url) {
        getStats().addTotalProcessedURL();
        if (!site.getDomain().equals(url.getDomain())) {
            return false;
        }
        if (hasSubdomain && !site.getSubDomain().equals(url.getSubDomain())) {
            return false;
        }
        return !filter.matcher(url.getURL().toLowerCase()).matches();
    }

    /**
     * @return the target
     */
    @Override
    public URLTarget getTarget() {
        return target;
    }

    /**
     * @return the stats
     */
    @Override
    public CrawlerStats getStats() {
        return stats;
    }

    @Override
    public void addBlockedPage() {
        blockedPages++;
    }

    @Override
    public int getBlockedPage() {
        return blockedPages;
    }

    @Override
    public final void resetBlockedPage() {
        blockedPages = 0;
    }
}
